/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	PrintSample

		 PrintSample.cpp

			Main entry point for the program.

			This module also includes the functions used to handle the
			messages sent to the application's main window.

 ------------------------------------------------------------------

 Copyright (c) 2010 Microsoft Corporation. All rights reserved.

 THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
 ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 
 PARTICULAR PURPOSE.

 ------------------------------------------------------------------
*/
#include "stdafx.h"
#include "PrintSample.h"

// forward declaration of functions used only in this module
ATOM MyRegisterClass(HINSTANCE hInstance, LPCWSTR szWindowClass);
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);

/** 
	<module>PrintSample.cpp</module>

	<summary>
		Entry point of the program.</summary>

	<param name="hInstance">
		Handle to the current instance of the application.</param>

	<param name="hPrevInstance">
		Handle to the previous instance of the application. This 
		parameter is always NULL.</param>

	<param name="lpCmdLine">
		Pointer to a null-terminated string specifying the command 
		line for the application, excluding the program name.</param>

	<param name="nCmdShow">
		Specifies how the window is to be shown.</param>

	<returns>
		The exit value contained in the WM_QUIT message's wParam 
		parameter. If the function terminates before entering the 
		message loop, it returns zero.
		</returns>

*/
int APIENTRY 
wWinMain(
		HINSTANCE hInstance, 
		HINSTANCE hPrevInstance, 
		LPWSTR lpCmdLine, 
		int nCmdShow
)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

	MSG msg;
	HACCEL hAccelTable;
	HRESULT hr = S_OK;

	// Register the main window class name
	WCHAR szWindowClass[MAXIMUM_RESOURCE_STRING_LENGTH];			
	LoadString(
		hInstance, 
		IDC_PRINTSAMPLE, 
		szWindowClass, 
		MAXIMUM_RESOURCE_STRING_LENGTH);
	MyRegisterClass(hInstance, szWindowClass);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		// Unable to initialize this instance of the application
		//  so display error message and exit
		MessageBoxWithResourceString (
			hInstance, 
			GetDesktopWindow(), 
			IDS_ERROR_INSTINITFAIL, 
			IDS_CAPTION_ERROR, 
			(MB_OK | MB_ICONEXCLAMATION));
		return FALSE;
	}	
	
	// Init COM for printing interfaces
    if (FAILED(hr = CoInitializeEx(0, COINIT_MULTITHREADED)))
    {
		// Unable to initialize COM
		//  so display error message and exit
		MessageBoxWithResourceString (
			hInstance, 
			GetDesktopWindow(), 
			IDS_ERROR_COMINITFAIL, 
			IDS_CAPTION_ERROR, 
			(MB_OK | MB_ICONEXCLAMATION));
		return FALSE;
    }

	hAccelTable = LoadAccelerators(
					hInstance, 
					MAKEINTRESOURCE(IDC_PRINTSAMPLE));

	// Main message handling loop
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	// Uninitialize (close) the COM interface
	CoUninitialize();

	return (int) msg.wParam;
}

/** 
	<module>PrintSample.cpp</module>

	<summary>
		Registers the window class.</summary>

	<param name="hInstance">
		Handle to the current instance of the application.</param>

	<param name="szWindowClass">
		The name of the window class to register.</param>

	<returns>
		The result returned by the RegisterWindowEx function.
		</returns>

	<remarks>
		This function and its usage are only necessary if you want 
		this code to be compatible with Win32 systems prior to the 
		'RegisterClassEx' function that was added to Windows 95. 
		It is important to call this function so that the application 
		will get 'well formed' small icons associated with it.
		</remarks>

*/
ATOM 
MyRegisterClass(
	HINSTANCE hInstance, 
	LPCWSTR szWindowClass
)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW | CS_DROPSHADOW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= sizeof(PPRINTTHREADINFO);
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(
							hInstance, 
							MAKEINTRESOURCE(IDI_PRINTSAMPLE));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_PRINTSAMPLE);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(
							wcex.hInstance, 
							MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

//
/** 
	<module>PrintSample.cpp</module>

	<summary>
		Saves instance handle and creates main window.</summary>

	<param name="hInstance">
		Handle to the current instance of the application.</param>

	<param name="nCmdShow">
		Specifies how the window is to be shown.</param>

	<returns>
		<p>TRUE if the function was successful.</p>
		<p>FALSE if an error occured.</p>
		</returns>

	<remarks>
        In this function, we save the instance handle in the 
		application's main data structure and create and display 
		the main program window.
		</remarks>

*/
BOOL 
InitInstance(
	HINSTANCE hInstance, 
	int nCmdShow
)
{
	HWND hWnd;

	// The title bar text
	WCHAR szTitle[MAXIMUM_RESOURCE_STRING_LENGTH];

	// The main window class name
	WCHAR szWindowClass[MAXIMUM_RESOURCE_STRING_LENGTH]; 

	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAXIMUM_RESOURCE_STRING_LENGTH);
	LoadString(hInstance, IDC_PRINTSAMPLE, szWindowClass, MAXIMUM_RESOURCE_STRING_LENGTH);

	// Create the main window
	hWnd = CreateWindow(
			szWindowClass, 
			szTitle, 
			WS_OVERLAPPEDWINDOW | WS_THICKFRAME,
			CW_USEDEFAULT,
			0, 
			CW_USEDEFAULT, 
			0, 
			NULL, 
			NULL, 
			hInstance, 
			NULL);

	if (NULL != hWnd)
	{
		// The main window was created.
		ShowWindow(hWnd, nCmdShow);
		UpdateWindow(hWnd);
		return TRUE;
	}
	else
	{
		// The main window was not created.
		return FALSE;
	}
}
